package com.lbx.dhr.user.application.serivce;

import com.github.pagehelper.PageInfo;
import com.lbx.dhr.user.application.converter.TestConverter;
import com.lbx.dhr.user.application.dto.TestDto;
import com.lbx.dhr.user.infra.entity.Test;
import com.lbx.dhr.user.infra.repo.TestRepo;
import com.lbx.framework.db.search.CommonSeg;
import com.lbx.framework.db.search.Condition;
import com.lbx.framework.db.search.ISeg;
import com.lbx.framework.db.search.Operator;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author daocers
 * @date 2024/1/20 17:35
 */
@Service
public class TestService {
    @Autowired
    TestRepo testRepo;

    public List<TestDto> getAll() {
        List<Test> list = testRepo.findByCondition(null);
        if (CollectionUtils.isNotEmpty(list)) {
            List<TestDto> res = list.stream().map(item -> {
                TestDto dto = TestConverter.entity2Dto(item);
                return dto;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            return res;
        }
        return new ArrayList<>();
    }


    /**
     * 条件查询， 不想写复杂sql的参考这里
     *
     * @param
     * @return
     * @todo todo  开发快来看这里
     * @author alei
     * @date 2024/1/20 18:14
     */
    public List<TestDto> testSeg() {
        Test query = new Test();
        query.setName("王五");

        ISeg seg = CommonSeg.AND()
                .add(Condition.of("id", Operator.NOT_EQUAL, "1"))
                .add(Condition.of("name", Operator.IN, "张三", "李四"));

//        todo todo   此处query中的等值条件和seg中的条件会同时生效。   tips： 简单理解， 1 entity中的 查询条件用于等值查询， 2 seg中的按照实际操作类型
        PageInfo<Test> pageInfo = testRepo.searchWithPage(1, 10, query, seg, null);
        if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
            List<TestDto> res = pageInfo.getList().stream().map(item -> {
                TestDto dto = TestConverter.entity2Dto(item);
                return dto;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            return res;
        }
        return new ArrayList<>();
    }

}
